import { getCampusSelector } from '@/api/campus/campus'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isEdit: false,
      dataForm: {
        id: undefined,
        brand: undefined,
        usableArea: undefined,
        classroomCount: undefined,
        degreeCount: undefined,
        dormitoryCount: undefined,
        bedCount: undefined,
        startDate: undefined,
        furation: undefined,
        designer: undefined,
        facade: [],
        floorPlan: [],
        status: 2
      },
      rules: {
        leasedArea: [
          {
            required: true,
            message: '请输入租用面积',
            trigger: 'blur'
          }
        ],
        usableArea: [
          {
            required: true,
            message: '请输入使用面积',
            trigger: 'blur'
          }
        ],
        classroomCount: [
          {
            required: true,
            message: '请输入教室数量',
            trigger: 'blur'
          },
          {
            pattern: /^\d+$/,
            message: '请输入正确的数字',
            trigger: 'blur'
          }
        ],
        degreeCount: [
          {
            required: true,
            message: '请输入学位数量',
            trigger: 'blur'
          },
          {
            pattern: /^\d+$/,
            message: '请输入正确的数字',
            trigger: 'blur'
          }
        ],
        dormitoryCount: [
          {
            required: true,
            message: '请输入宿舍数量',
            trigger: 'blur'
          },
          {
            pattern: /^\d+$/,
            message: '请输入正确的数字',
            trigger: 'blur'
          }
        ],
        bedCount: [
          {
            required: true,
            message: '请输入床位数量',
            trigger: 'blur'
          },
          {
            pattern: /^\d+$/,
            message: '请输入正确的数字',
            trigger: 'blur'
          }
        ],
        startDate: [
          {
            required: true,
            message: '请输入开工日期',
            trigger: 'change'
          }
        ],
        furation: [
          {
            required: true,
            message: '请输入施工时长',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的数字',
            trigger: 'blur'
          }
        ],
        designer: [
          {
            required: true,
            message: '请输入设计师',
            trigger: 'change'
          }
        ],
        rent: [
          {
            required: true,
            message: '请输入场地月租金',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        facade: [
          {
            required: true,
            message: '请输入施工门头图',
            trigger: 'click'
          }
        ],
        floorPlan: [
          {
            required: true,
            message: '请输入施工平面图',
            trigger: 'click'
          }
        ]
      },

      campusOptions: [],
      campusLoading: false,
      brandOptions: []
    }
  },
  computed: {},
  watch: {
    'dataForm.brand': {
      async handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          try {
            this.campusLoading = true
            const res = await getCampusSelector({ brand: this.dataForm.br, fields: 'id,fullName' })
            const list = Array.isArray(res.data) ? res.data : []
            if (!this.dataForm.id) {
              this.campusOptions = list.filter(i => i.status != 'close' && i.status != 'expires')
            } else {
              this.campusOptions = list
            }
          } catch (error) {
            console.error('加载校区数据失败:', error)
            this.$message.error('校区选项加载失败')
            this.campusOptions = []
          } finally {
            this.campusLoading = false
          }
        }
      }
    }
  },
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  mounted() {
    // this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
    //   this.brandOptions = res
    // })
  },
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(id, isEdit, row) {
      this.dataForm.id = id || undefined
      this.visible = true
      this.isEdit = isEdit || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        request({
          url: '/api/campus/decoration/' + this.dataForm.id,
          method: 'get'
        }).then(res => {
          this.dataForm = res.data
          if (!this.dataForm.facade) this.dataForm.facade = []
          if (!this.dataForm.floorPlan) this.dataForm.floorPlan = []
        })
        console.log(this.dataForm)
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (!this.isEdit) {
            request({
              url: `/api/campus/decoration`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/campus/decoration/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
